package com.paas.form.handler;

import com.alibaba.fastjson2.JSONObject;
import com.paas.common.jdbc.ResultHandler;
import com.paas.common.jdbc.model.DbColumn;
import com.paas.common.jdbc.model.DbConfig;
import com.paas.common.jdbc.model.DbResult;
import com.paas.common.model.DataBaseEntity;
import com.paas.common.util.AppUtil;
import com.paas.common.util.BeanUtil;
import com.paas.common.util.StringUtil;
import com.paas.plus.tool.form.model.View;
import com.paas.plus.tool.realTimeDistributed.model.IncrementalDistribution;
import com.paas.plus.tool.realTimeDistributed.model.RealTimeDistributeConfig;
import com.paas.plus.tool.realTimeDistributed.model.RealTimeDistributeExecutionParameter;
import com.paas.plus.tool.realTimeDistributed.server.IRealTimeDistributeServer;

import java.util.*;

public class FormJobTaskHandler implements ResultHandler {

    private List<IncrementalDistribution> incrementalDistributionList;

    private RealTimeDistributeConfig realTimeDistributeConfig;

    private int count = 0;

    private int failedCount = 0;

    private String jobId;

    private DataBaseEntity db;

    private View view;

    private DbResult dbResult;

    private IRealTimeDistributeServer realTimeDistributeServer;

    private Map<String,String> columnMapperMap;

    @Override
    public void handleResult(Map<String, Object> data) {
        String idField = view.getIdField();
        String id = null;
        HashMap<String, Object> tempMap = new HashMap<>();
        Iterator<Map.Entry<String, Object>> iterator = data.entrySet().iterator();
        //遍历获取执行参数map
        while (iterator.hasNext()){
            Map.Entry<String, Object> next = iterator.next();
            if (columnMapperMap.containsKey(next.getKey().toLowerCase()) || columnMapperMap.containsKey(next.getKey().toUpperCase())){
                if (StringUtil.isNotEmpty(columnMapperMap.get(next.getKey().toUpperCase()))){
                    tempMap.put(columnMapperMap.get(next.getKey().toUpperCase()),next.getValue());
                }else if (StringUtil.isNotEmpty(columnMapperMap.get(next.getKey().toLowerCase()))){
                    tempMap.put(columnMapperMap.get(next.getKey().toLowerCase()),next.getValue());
                }
            }
        }
        //判断当前参数中,是否包含视图id
        if (tempMap.containsKey(idField.toUpperCase()) || tempMap.containsKey(idField.toLowerCase())){
            if (BeanUtil.isNotEmpty(tempMap.get(idField.toUpperCase()))){
                id = tempMap.get(idField.toUpperCase()).toString();
            }else {
                id = tempMap.get(idField.toLowerCase()).toString();
            }

            DbConfig config = new DbConfig(db.getJdbcDrivers(), db.getJdbcUrl(), db.getJdbcUser(), db.getJdbcPassword(),db.getDbName());
            IncrementalDistribution incrementalDistribution = new IncrementalDistribution();
            incrementalDistribution.setDbConfig(config);
            incrementalDistribution.setValueMap(tempMap);
            incrementalDistribution.setTableName(view.getLinkTable());
            incrementalDistribution.setId(id);
            RealTimeDistributeExecutionParameter realTimeDistributeExecutionParameter = new RealTimeDistributeExecutionParameter();
            realTimeDistributeExecutionParameter.setJobId(jobId);
            realTimeDistributeExecutionParameter.setPrimaryKeyField(idField);
            incrementalDistribution.setRealTimeDistributeConfig(realTimeDistributeConfig);
            incrementalDistribution.setRealTimeDistributeExecutionParameter(realTimeDistributeExecutionParameter);
            if (incrementalDistributionList==null){
                incrementalDistributionList = new ArrayList<>();
            }
            incrementalDistributionList.add(incrementalDistribution);
            if (incrementalDistributionList.size()>=200){
                JSONObject object = getRealTimeDistributeServer().executeIncrementalDistribution(incrementalDistributionList);
                failedCount += Integer.valueOf(object.get("failedCount").toString());
                incrementalDistributionList.clear();
            }
            count++;
        }
    }

    @Override
    public void setColumns(List<DbColumn> dbColumnList) {}

    public Map<String, String> getColumnMapperMap() {
        return columnMapperMap;
    }

    public void setColumnMapperMap(Map<String, String> columnMapperMap) {
        this.columnMapperMap = columnMapperMap;
    }

    public RealTimeDistributeConfig getRealTimeDistributeConfig() {
        return realTimeDistributeConfig;
    }

    public void setRealTimeDistributeConfig(RealTimeDistributeConfig realTimeDistributeConfig) {
        this.realTimeDistributeConfig = realTimeDistributeConfig;
    }

    public int getFailedCount() {
        return failedCount;
    }

    public void setFailedCount(int failedCount) {
        this.failedCount = failedCount;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public DbResult getDbResult() {
        return dbResult;
    }

    public void setDbResult(DbResult dbResult) {
        this.dbResult = dbResult;
    }

    public DataBaseEntity getDb() {
        return db;
    }

    public void setDb(DataBaseEntity db) {
        this.db = db;
    }

    public View getView() {
        return view;
    }

    public void setView(View view) {
        this.view = view;
    }

    public String getJobId() {
        return jobId;
    }

    public void setJobId(String jobId) {
        this.jobId = jobId;
    }

    private IRealTimeDistributeServer getRealTimeDistributeServer(){
        if (BeanUtil.isEmpty(realTimeDistributeServer)){
            realTimeDistributeServer = (IRealTimeDistributeServer) AppUtil.getBean(IRealTimeDistributeServer.class);
        }
        return realTimeDistributeServer;
    }

    public List<IncrementalDistribution> getIncrementalDistributionList() {
        return incrementalDistributionList;
    }

    public void setIncrementalDistributionList(List<IncrementalDistribution> incrementalDistributionList) {
        this.incrementalDistributionList = incrementalDistributionList;
    }
}
